<?php
/**
 * File: BaseMessage.class.php
 * Author: camy <375766145@qq.com>
 * Time: 2018/3/21 18:15
 */

namespace Common\Extend\DingTalk\Message;

abstract class BaseMessage
{
    protected $host;
    protected $picUrl;
    protected $title;

    public function __construct()
    {
        $this->title = '消息提醒';
        $this->host = U('service/auth', [], false, true);
        $this->picUrl = '@lALOACZwe2Rk';
    }

    /**
     * 发送消息
     * @param $param
     */
    abstract public function send($param);

    protected function setUrl($route, $args = [])
    {
        $url = U($route, $args, false, true);
        $this->host = $this->host . '?callback=' . urlencode($url);
    }

    protected function getMessage($text)
    {
        $template =
            '{
        "messageUrl": "' . $this->host . '",
        "picUrl":"' . $this->picUrl . '",
        "title": "' . $this->title . '",
        "text": "' . $text . '\n点击此处查看更多>>>"
        }';
        return $template;
    }

    protected function getUserId($ids)
    {
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $userIdArr = M('user')
            ->field('userid')
            ->where(['id' => ['in', $ids]])
            ->select();
        return array_column($userIdArr, 'userid');
    }
}